﻿using System.Collections.Generic;
namespace CharpClassGen
{
    //属性
    public class Property : Member
    {
        public Field field;
        public bool get;
        public bool set;
        public bool genField;
        public bool privateSet;
        public List<string> getCodeBlock = new List<string>();
        public List<string> setCodeBlock = new List<string>();
       
        public void AddFieldAttribute(string attribute)
        {
            this.field.attributes.Add(attribute);
        }

        public Property AddGetCodeBlock(string codeBlock)
        {
            this.getCodeBlock.Add(codeBlock);
            return this;
        }


        public Property AddSetCodeBlock(string codeBlock)
        {
            this.setCodeBlock.Add(codeBlock);
            return this;
        }
        public Property(string type, string name, bool get = true, bool set = true, bool privateSet=true, bool genField = true, Accesser accesser = Accesser.Public, ExtendModifier extendModifier = ExtendModifier.none) : base(accesser, extendModifier, type, name)
        {
            field = new Field(type, "_" + name, Accesser.Private, extendModifier);

            this.get = get;
            this.set = set;
            this.genField = genField;
            this.privateSet = privateSet;
        }

        public override string ToString()
        {
            base.ToString();
            if (genField)
            {
                field.retrat = this.retrat;
                sb.Insert(0, field.ToString());
            }
            sb.AppendLine();


            sb.AppendLine($"{Helper. Retract(retrat)}{{");
            if (get)
            {
                //Get
                sb.AppendLine($"{Helper.Retract(retrat + 1)}get{{");
                if (getCodeBlock.Count == 0)
                {
                    sb.AppendLine($"{Helper.Retract(retrat + 2)} return {field.name} ;");
                }
                else
                {
                    foreach (var item in getCodeBlock)
                    {
                        sb.AppendLine($"{Helper.Retract(retrat)} {item} ");
                    }
                }
                sb.AppendLine($"{Helper.Retract(retrat + 1)}}}");
            }


            //Set
            if (set)
            {
                string privateStr = privateSet ? "private" : "";

                sb.AppendLine($"{Helper.Retract(retrat + 1)}  {privateStr}   set{{");
                if (setCodeBlock.Count == 0)
                {
                    sb.AppendLine($"{Helper.Retract(retrat + 2)}{field.name}=value ;");
                }
                else
                {
                    foreach (var item in setCodeBlock)
                    {
                        item.Split("\n");
                        sb.AppendLine($"{Helper.Retract(retrat + 2)}{item} ");
                    }
                }

                sb.AppendLine($"{Helper.Retract(retrat + 1) }}}");
            }
            sb.AppendLine($"{Helper.Retract(retrat)}}}");

            return sb.ToString();
        }

    }
}



